<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2016/11/20
 * Time: 22:56
 */
function dbInit(){
    $link=mysqli_connect('localhost','root','','itacst');
    if(!$link){
        die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
    }
    mysqli_query($link,'set names utf8');
    return $link;
}
function query($link,$sql) {
    $link=mysqli_connect('localhost','root','','itcast');
    if ($result = mysqli_query($link,$sql)) {
        return $result;
    } else {
        echo '错误的SQL为:', $sql, '<br>';
        echo die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
    }
}
function fetchAll($link,$sql) {
    if ($result = query($link,$sql)) {
        $rows = array();
        while( $row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
            $rows[] = $row;
        }
        mysqli_free_result($result);
        return $rows;
    } else {
        return false;
    }
}
function fetchRow($sql) {
    if ($result = query($sql)) {
        $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
        return $row;
    } else {
        return false;
    }
}
function safeHandle($data,$link){
    $data = htmlspecialchars($data);
    $data = mysqli_real_escape_string($data,$link);
    return $data;
}